package com.xuxin.pda.network;

import com.xuxin.pda.base.BaseConstant;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xu_xin
 * Created on 2020.06.05
 */
public class Retrofit {
    private static final String baseUrl = "http://39.104.74.183:21028";
    private static Retrofit mRetrofits;
    private retrofit2.Retrofit mRetrofit;

    private Retrofit() {
        initRetrofit();
    }

    public static synchronized Retrofit getInstance() {
        if (mRetrofits == null) {
            mRetrofits = new Retrofit();
        }
        return mRetrofits;
    }

    private void initRetrofit() {
        HttpLoggingInterceptor LoginInterceptor = new HttpLoggingInterceptor();
        LoginInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        if (BaseConstant.isDebug) {
            builder.addInterceptor(LoginInterceptor);
        }
        builder.connectTimeout(BaseConstant.timeOut, TimeUnit.SECONDS);
        builder.readTimeout(BaseConstant.timeOut, TimeUnit.SECONDS);
        builder.writeTimeout(BaseConstant.timeOut, TimeUnit.SECONDS);
        builder.retryOnConnectionFailure(true);
        OkHttpClient client = builder.build();
        mRetrofit = new retrofit2.Retrofit.Builder().baseUrl(Retrofit.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create()).client(client).build();
    }

    public <T> T createReq(Class<T> reqServer) {
        return mRetrofit.create(reqServer);
    }
}